/*
 * (C) Copyright 2014 mjahnen <github@mgns.tech>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package me.jahnen.libaums.core.driver.scsi.commands

import android.util.Log
import java.nio.ByteBuffer

/**
 * This class represents a SCSI Inquiry command. It is used to get important
 * information about the connected mass storage device. This information include
 * the supported SCSI commands.
 *
 *
 * The response is sent in the data phase.
 *
 * @author mjahnen
 * @see com.github.mjdev.libaums.driver.scsi.commands.ScsiInquiryResponse
 */
class ScsiInquiry(private val allocationLength: Byte, lun: Byte) : CommandBlockWrapper(allocationLength.toInt(), Direction.IN, lun, LENGTH) {

    override fun serialize(buffer: ByteBuffer) {
        super.serialize(buffer)
        buffer.apply {
            put(OPCODE)
            put(0.toByte())
            put(0.toByte())
            put(0.toByte())
            put(allocationLength)
        }
    }

    companion object {
        private const val LENGTH: Byte = 0x6
        private const val OPCODE: Byte = 0x12

        private const val SET_OPCODE: Byte = 238.toByte() //OpCode信息，头部必须，查询 0xee
        private const val GET_OPCODE: Byte = 239.toByte() //OpCode信息，头部必须，查询 0xef

        const val CODE_SET_LIGHT: Int = 2 //Indicator_Light
        const val CODE_SET_WIFI: Int = 6 //操作工牌的 wifi 开关

        const val SET_CLOSE: Int = 48
        const val SET_OPEN: Int = 49 //0x30=48 0x31=49

        const val SettingTwo_Type1: Int = 880
        const val SettingTwo_Type2: Int = 881
    }

    fun customTwoSetting(buffer: ByteBuffer,customCom: Int,type: Int,data: Int = 0){
        super.serialize(buffer)
        Log.e("TAG","allocationLength:"+allocationLength+","+data)
        when(type){
            SettingTwo_Type1 -> { //无返回
                buffer.apply {
                    put(SET_OPCODE)
                    put(0.toByte())//无分包
                    put(customCom.toByte())
                    put(0.toByte())//size，重启不需要，是0
                    put(allocationLength)
                    put(data.toByte())
                }
            }
            SettingTwo_Type2 -> { //需要设定参数
                buffer.apply {
                    put(SET_OPCODE)
                    put(0.toByte())//无分包
                    put(customCom.toByte())
                    put(1.toByte())//size
                    put(allocationLength)
                    put(data.toByte())//开关 0x30=48 0x31=49
                }
            }
        }
    }
    fun custom(buffer: ByteBuffer,data: ByteArray){
        super.serialize(buffer)
        //0xee 00 02 01 20 31
        buffer.put(data)
//        buffer.put(GET_OPCODE)
//        buffer.put(0.toByte())//无分包
//        buffer.put(customCom.toByte())
//        buffer.put(0.toByte())//size，重启不需要，是0
//        buffer.put(allocationLength)
//        buffer.put(data.toByte())

    }

}
